package config

import (
	"git.multiverse.io/eventkit/sed/callback"
	"git.multiverse.io/framework/log"
)

var Cfg *DxcConfig

type DxcConfig struct {
	ServerAddress string
	CallbackPort  int
	HttpPort      int
	NodeId        string
	InstanceId    string
	Log           LoggerConfig
	Heartbeat     HeartbeatConfig
	Service       ServiceConfig
	Job           JobConfig
	Repo          RepoConfig
	Metrics       MetricsConfig
	Management    ManagementConfig
}

type ServiceConfig struct {
	Org                            string
	Su                             string
	Az                             string
	Wks                            string
	Env                            string
	TxnBeginTopicId                string
	TxnJoinTopicId                 string
	TxnEndTopicId                  string
	ExpireTimeSeconds              int64
	MaxCallbackTimeoutMilliseconds int
	MaxCallbackRetryTimes          int
	MaxServiceConsumeMilliseconds  int
	CallbackNodeId                 string
	MaxCallbackChannel             int
	IgnoreCheckClientSDKVersion    bool
	IgnoreClientSDKVersionWithDxc  bool
}

type JobConfig struct {
	MinIntervalSeconds int64
	DurationSeconds    int64
	BatchSyncSize      int64
}

type RepoConfig struct {
	Type  string
	Redis struct {
		Type     string
		Addr     []string
		Password string
	}
	Hazelcast struct {
		Addr []string
	}
}

type LoggerConfig struct {
	LogFile            string
	LogLevel           string
	LogLevelUnixSocket string
	FilePath           string
	MaxSize            int
	MaxDays            int
	MaxBackups         int
	Console            bool
}

type MetricsConfig struct {
	Enable                bool
	ReportIntervalSeconds int
	ReportToTopicID       string
}

type ManagementConfig struct {
	Enable                                                       bool
	AbnormalTransactionRecordReportTopicID                       string
	AbnormalTransactionRecordReportTimeoutMilliseconds           int
	RedundancyAbnormalTransactionRecordReportTopicID             string
	RedundancyAbnormalTransactionRecordReportTimeoutMilliseconds int
	PageQueryRedundancyAbnormalTxnRecordTopicID                  string
	PageQueryRedundancyAbnormalTxnRecordTimeoutMilliseconds      int
}

type HeartbeatConfig struct {
	TopicName       string
	IntervalSeconds int
}

func (c *DxcConfig) GenLogConfig() log.Config {
	return log.Config{
		Rotate: log.RotateConfig{
			FilePath:   c.Log.FilePath,
			Filename:   c.Log.LogFile,
			MaxSize:    c.Log.MaxSize,
			MaxBackups: c.Log.MaxBackups,
			MaxAge:     c.Log.MaxDays,
		},
		Level:   c.Log.LogLevel,
		Console: c.Log.Console,
	}
}

func (c *DxcConfig) GenInitConfig() []callback.Option {
	options := make([]callback.Option, 0)
	options = append(options, callback.WithPort(c.HttpPort))
	options = append(options, callback.WithCallbackPort(c.CallbackPort))
	options = append(options, callback.WithServerAddress(c.ServerAddress))
	return options
}
